using System;
using System.Collections.Generic;
using System.Linq;
namespace WebClient.Models.Repositories
{
	#region using
	using WebClient.Helpers;
	using WebClient.Models.ViewModels;
	using WebClient.Models;
    using WebClient.CF_DAL_WS;
	#endregion
	public class DistrictRepository
	{
        public List<DistrictDTO> SelectAll()
		{
			try
			{
                return DALService.Instance.District_SelectAll().ToList();
			}
			catch(Exception ex)
			{
				if(ex.InnerException != null)
					GlobalVariables.Logger.Error("District - SelectAll: " + ex.InnerException.Message.Replace("\t", string.Empty).Replace("\r", string.Empty).Replace("\n", string.Empty));
				else
					GlobalVariables.Logger.Error("District - SelectAll: " + ex.Message.Replace("\t", string.Empty).Replace("\r", string.Empty).Replace("\n", string.Empty));
			}
            return new List<DistrictDTO>();
		}

		public DistrictDTO SelectByID(int id)
		{
			try
			{
                return DALService.Instance.District_SelectByID(id.ToString());
			}
			catch(Exception ex)
			{
				if(ex.InnerException != null)
					GlobalVariables.Logger.Error("District - SelectByID: " + ex.InnerException.Message.Replace("\t", string.Empty).Replace("\r", string.Empty).Replace("\n", string.Empty));
				else
					GlobalVariables.Logger.Error("District - SelectByID: " + ex.Message.Replace("\t", string.Empty).Replace("\r", string.Empty).Replace("\n", string.Empty));
			}
            return null;
		}

        public List<DistrictDTO> SelectPaging(int page, int pageSize)
		{
			try
			{
                var lstItem = DALService.Instance.District_SelectAll().ToList();
                if (pageSize > 0)
                {
                    int startIndex, itemCount;
                    startIndex = (page - 1) * pageSize;
                    itemCount = pageSize;
                    if (startIndex + itemCount > lstItem.Count)
                        itemCount = lstItem.Count - startIndex;
                    lstItem = lstItem.GetRange(startIndex, itemCount);
                }
                return lstItem;
			}
			catch(Exception ex)
			{
				if(ex.InnerException != null)
					GlobalVariables.Logger.Error("District - SelectPaging: " + ex.InnerException.Message.Replace("\t", string.Empty).Replace("\r", string.Empty).Replace("\n", string.Empty));
				else
					GlobalVariables.Logger.Error("District - SelectPaging: " + ex.Message.Replace("\t", string.Empty).Replace("\r", string.Empty).Replace("\n", string.Empty));
			}
            return new List<DistrictDTO>();
		}

		public bool Insert(DistrictDTO obj)
		{
			try
			{
                return DALService.Instance.District_Insert(obj);
			}
			catch(Exception ex)
			{
				if(ex.InnerException != null)
					GlobalVariables.Logger.Error("District - Insert: " + ex.InnerException.Message.Replace("\t", string.Empty).Replace("\r", string.Empty).Replace("\n", string.Empty));
				else
					GlobalVariables.Logger.Error("District - Insert: " + ex.Message.Replace("\t", string.Empty).Replace("\r", string.Empty).Replace("\n", string.Empty));
			}
            return false;
		}

		public bool Delete(int id)
		{
			try
			{
                return DALService.Instance.District_Delete(id.ToString());
			}
			catch(Exception ex)
			{
				if(ex.InnerException != null)
					GlobalVariables.Logger.Error("District - Delete: " + ex.InnerException.Message.Replace("\t", string.Empty).Replace("\r", string.Empty).Replace("\n", string.Empty));
				else
					GlobalVariables.Logger.Error("District - Delete: " + ex.Message.Replace("\t", string.Empty).Replace("\r", string.Empty).Replace("\n", string.Empty));
			}
            return false;
		}

        public bool Update(DistrictDTO obj)
        {
            try
            {
                return DALService.Instance.District_Update(obj);
            }
            catch
            {
                return false;
            }
        }

		public int GetTotalItem()
		{
			try
			{
                return DALService.Instance.District_SelectAll().Length;
			}
			catch(Exception ex)
			{
				if(ex.InnerException != null)
					GlobalVariables.Logger.Error("District - GetTotalItem: " + ex.InnerException.Message.Replace("\t", string.Empty).Replace("\r", string.Empty).Replace("\n", string.Empty));
				else
					GlobalVariables.Logger.Error("District - GetTotalItem: " + ex.Message.Replace("\t", string.Empty).Replace("\r", string.Empty).Replace("\n", string.Empty));
			}
            return -1;
		}

		public int GetTotalItemByIDCity(int idcity)
		{
			try
			{
                return DALService.Instance.District_SelectByIDCity(idcity.ToString()).Length;
			}
			catch(Exception ex)
			{
				if(ex.InnerException != null)
					GlobalVariables.Logger.Error("District - GetTotalItemByIDCity: " + ex.InnerException.Message.Replace("\t", string.Empty).Replace("\r", string.Empty).Replace("\n", string.Empty));
				else
					GlobalVariables.Logger.Error("District - GetTotalItemByIDCity: " + ex.Message.Replace("\t", string.Empty).Replace("\r", string.Empty).Replace("\n", string.Empty));
			}
            return -1;
		}


		public int GetTotalPage()
		{
			int RecordCount = GetTotalItem();
			int PageSize = WebConfiguration.NumDistrictPerPage;
			return (RecordCount / PageSize) + ((RecordCount % PageSize == 0) ? 0 : 1);
		}

		public int GetTotalPageByIDCity(int idcity)
		{
			try
			{
				int RecordCount = GetTotalItemByIDCity(idcity);
				int PageSize = WebConfiguration.NumDistrictPerPage;
				return (RecordCount / PageSize) + ((RecordCount % PageSize == 0) ? 0 : 1);
			}
			catch(Exception ex)
			{
				if(ex.InnerException != null)
					GlobalVariables.Logger.Error("District - GetTotalPageByIDCity: " + ex.InnerException.Message.Replace("\t", string.Empty).Replace("\r", string.Empty).Replace("\n", string.Empty));
				else
					GlobalVariables.Logger.Error("District - GetTotalPageByIDCity: " + ex.Message.Replace("\t", string.Empty).Replace("\r", string.Empty).Replace("\n", string.Empty));
			}
            return -1;
		}


		public List<DistrictDTO> RetrieveByID(int id)
		{
			try
			{
                return DALService.Instance.District_RetrieveByID(id.ToString()).ToList();
			}
			catch(Exception ex)
			{
				if(ex.InnerException != null)
					GlobalVariables.Logger.Error("District - RetrieveByID: " + ex.InnerException.Message.Replace("\t", string.Empty).Replace("\r", string.Empty).Replace("\n", string.Empty));
				else
					GlobalVariables.Logger.Error("District - RetrieveByID: " + ex.Message.Replace("\t", string.Empty).Replace("\r", string.Empty).Replace("\n", string.Empty));
			}
            return new List<DistrictDTO>();
		}

		public List<DistrictDTO> SelectByIDCity(int idcity)
		{
			try
			{
                return DALService.Instance.District_SelectByIDCity(idcity.ToString()).ToList();
			}
			catch(Exception ex)
			{
				if(ex.InnerException != null)
					GlobalVariables.Logger.Error("District - SelectByIDCity: " + ex.InnerException.Message.Replace("\t", string.Empty).Replace("\r", string.Empty).Replace("\n", string.Empty));
				else
					GlobalVariables.Logger.Error("District - SelectByIDCity: " + ex.Message.Replace("\t", string.Empty).Replace("\r", string.Empty).Replace("\n", string.Empty));
			}
            return new List<DistrictDTO>();
		}

	}
}